package com.hust.bridgecheck.controller.BridgeSensor;

import com.hust.bridgecheck.entity.*;
import com.hust.bridgecheck.service.BridgeSensor.BridgeService;
import com.hust.bridgecheck.service.BridgeSensor.SensorService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("bridge")
public class BridgeSensorController {
    @Autowired
    private SensorService sensorService;
    @Autowired
    private BridgeService bridgeService;

    @PostMapping("/realtimesensordata")
    private List<SensorData> getRealTimeData( @RequestParam("paraid") String paraId,@RequestParam("starttime") String starttime, @RequestParam("endtime") String endtime){
        //获取数据对象
        return sensorService.getSensorDataByParaId(paraId,starttime,endtime);
    }

    @PostMapping("/realtimewarningdata")
    private List<WarningData> getWarningData(@RequestParam("paraid") String paraId){
        return sensorService.getWarningDataByParaId(paraId);
    }

    @PostMapping("/allwarningdata")
    private List<WarningData> getBridgeWarningData(@RequestParam("bridgeid") Integer bridgeid){
        return sensorService.getWarningDataByBridgeId(bridgeid);
    }

    @PostMapping("/allearlywarning")
    private List<EarlyWarning> getBridgeEarlyWarning(@RequestParam("bridgeid") Integer bridgeid){
        return sensorService.getEarlyWarningByBridgeId(bridgeid);
    }

    @PostMapping("/releasewarning")
    private int ReleaseBridgeWarning(@RequestParam("earlywarnigid") Integer earlywarningid){
        return sensorService.deleteEarlyWarningById(earlywarningid);
    }

    @PostMapping("/getallreport")
    private List<DailySensorData> getReportByBridge(@RequestParam("BID") Integer BridgeID, @RequestParam("StartTime") String Startime, @RequestParam("EndTime") String Endtime){
        return sensorService.getDailySensorData(BridgeID,Startime,Endtime);
    }

    @PostMapping("/getreportdetail")
    private List<DailySensorData> getReportByGroup(@RequestParam("BID") Integer BridgeID, @RequestParam("StartTime") String Startime, @RequestParam("EndTime") String Endtime){
        return sensorService.getSumDailySensorData(BridgeID,Startime,Endtime);
    }

    @PostMapping("/getmultidata")
    private List<WarningData> getMultiWarningData(@RequestParam("DataID") String DataID){
        List<Integer> MultiDataID = new ArrayList<>();
        String[] First = DataID.split(",");//第一层分割，使用,作为分隔符
        //循环去取，然后进行再分割
        for(int i = 0; i < First.length; i++){
            String[] Second = First[i].split(";");
            for(int j = 0; j < Second.length; j++){
                MultiDataID.add(Integer.parseInt(Second[j]));
            }
        }
        //先对进来的数据进行分割
        return sensorService.getMultiDailySenosorData(MultiDataID);
    }

    @GetMapping("/domainbridge/{DID}")
    private List<Object> getDomainBrigde(Model model, @PathVariable("DID") Integer domainID){
        return bridgeService.getDomainBridge(domainID);
    }

    @GetMapping("/viewBridgeByID/{BID}")
    private Bridge showBridgeByID(Model model, @PathVariable("BID") Integer BridgeID){
        return bridgeService.getBridgeByID(BridgeID);
    }

    @GetMapping("/bridgesensor/{BID}")
    private List<Sensor> getBridgeSensor(Model model,@PathVariable("BID") Integer BID){
        return sensorService.getBridgeSenors(BID);
    }

    @PostMapping("/addWarningData")
    private WarningData addWarningData(WarningData warningData){
        return sensorService.addWarningData(warningData);
    }

    @GetMapping("/getEarlyWarningByPara/{PID}")
    private EarlyWarning getEarlyWarning(@PathVariable("PID") String PID){
        return sensorService.getEarlyWarningByParaID(PID);
    }

    @PostMapping("/addEarlyWarning")
    private EarlyWarning addEarlyWarning(EarlyWarning earlyWarning){
        return sensorService.addEarlyWarning(earlyWarning);
    }
}
